//
//  Html5ViewController.m
//  LazyCat
//
//  Created by zhanshu on 15/9/10.
//  Copyright (c) 2015年 zhanshu. All rights reserved.
//

#import "Html5ViewController.h"
#import "NJKWebViewProgress.h"
#import "NJKWebViewProgressView.h"

@interface Html5ViewController ()<UIWebViewDelegate,NJKWebViewProgressDelegate>
{
    float length;
    NSMutableData *webData;
    NJKWebViewProgressView *_progressView;
    NJKWebViewProgress *_progressProxy;
    BOOL _finishLoad;//web页是否加载完毕
    __weak IBOutlet UIView *noNetWorkView;
}
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (weak, nonatomic) IBOutlet UIProgressView *progress;
@property (nonatomic,strong) UIView *noNetView;//无网络视图

@end

@implementation Html5ViewController

-(void)viewWillAppear:(BOOL)animated
{
    // 进度条
    [super viewWillAppear:animated];
    [self.navBar addSubview:_progressView];
    
    if (_doComeFromeHomeView) {
        UIButton *shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        shareBtn.frame = CGRectMake(ScreenWidth-8-18, 20+(44-18)/2, 18, 18);
        [shareBtn setImage:[UIImage imageNamed:@"refresh_btn.png"] forState:UIControlStateNormal];
        [shareBtn setHitTestEdgeInsets:UIEdgeInsetsMake(-10, -20, -10, -10)];
        [shareBtn addTarget:self action:@selector(refresh) forControlEvents:UIControlEventTouchUpInside];
        [self.navBar addSubview:shareBtn];
    }else {
        //是商品详情页 显示分享按钮 否则不显示
        if (_doDetailType) {
            [self initShareBtn];
        }
    }
}
- (void)viewDidAppear:(BOOL)animated {
    

    if (kNetworkNotReachability) {
        [[MessShowView share] showWithMess:@"当前网络不给力"];
        self.webView.hidden = YES;
        self.noNetView.hidden = NO;
    }else {
        self.noNetView.hidden = YES;
        self.webView.hidden = NO;
//        [self loadMessPart];
//        [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.requestUrl]]];
    }
}
- (void)refresh {//刷新数据
    [_webView reload];
}
- (void)backAction:(UIButton *)btn {//判断是否可以HTML返回
    if (![_webView canGoBack]) {
        if (!self.navigationController.viewControllers) {
            [self dismissViewControllerAnimated:YES completion:^{
                
            }];
        }else {
            [self.navigationController popViewControllerAnimated:YES];
        }
    }else {
        [_webView goBack];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BackColor;
    _webView.backgroundColor = BackColor;
    [self initNav:_titleStr doBack:YES];
    UIView *lineV = [[UIView alloc]initWithFrame:CGRectMake(0, 63, ScreenWidth, 1)];
    lineV.backgroundColor = [UIColor lightGrayColor];
    lineV.alpha = 0.2;
    [self.view addSubview:lineV];
    [self loadMessPart];
    [self requestPart];
    self.noNetView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight - 104)];
    self.noNetView.hidden = YES;
    self.noNetView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.noNetView];
    UIImageView *netImg = [[UIImageView alloc] initWithFrame:CGRectMake((ScreenWidth-100)/2, 70, 100, 90)];
    netImg.image = [UIImage imageNamed:@"无网络_r1_c1"];
    [self.noNetView addSubview:netImg];
    UILabel *netFail = [[UILabel alloc] initWithFrame:CGRectMake(0, 180, ScreenWidth, 20)];
    netFail.text = @"网络请求失败";
    netFail.textAlignment = NSTextAlignmentCenter;
    netFail.font = [UIFont systemFontOfSize:16];
    [self.noNetView addSubview:netFail];
    UIButton *refresh = [[UIButton alloc] initWithFrame:CGRectMake((ScreenWidth-120)/2, 210, 120, 40)];
    [refresh setImage:[UIImage imageNamed:@"无网络_r3_c2 下午4.23.04"] forState:UIControlStateNormal];
    refresh.adjustsImageWhenHighlighted = NO;
    [self.noNetView addSubview:refresh];
    [refresh addTarget:self action:@selector(refreshView) forControlEvents:UIControlEventTouchUpInside];//刷新
}
-(void)refreshView {
    if (kNetworkNotReachability) {
        [[MessShowView share] showWithMess:@"当前网络不给力"];
        self.webView.hidden = YES;
        self.noNetView.hidden = NO;
    }else {
        self.noNetView.hidden = YES;
        self.webView.hidden = NO;
        [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.requestUrl]]];
    }
}
- (void)initShareBtn {
    UIButton *shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    shareBtn.frame = CGRectMake(ScreenWidth-8-25, 20+(44-18)/2, 20, 20);
    [shareBtn setImage:[UIImage imageNamed:@"fenfen.png"] forState:UIControlStateNormal];
    [shareBtn setHitTestEdgeInsets:UIEdgeInsetsMake(-10, -20, -10, -10)];
    [shareBtn addTarget:self action:@selector(shareAction) forControlEvents:UIControlEventTouchUpInside];
    [self.navBar addSubview:shareBtn];
}
- (void)loadMessPart {
#pragma mark -- 进度条1
    _progressProxy = [[NJKWebViewProgress alloc]init];
    _webView.delegate = _progressProxy;
    _progressProxy.webViewProxyDelegate = self;
    _progressProxy.progressDelegate = self;
    CGRect navBounds = self.navBar.bounds;
    CGRect barFrame = CGRectMake(0, navBounds.size.height - 2, 0, 2);
    _progressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];
    _progressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    [UIView animateWithDuration:1 animations:^{
        _progressView.frame = CGRectMake(_progressView.frame.origin.x, _progressView.frame.origin.y, ScreenWidth - ScreenWidth/2+80, 2);
    } completion:^(BOOL finished) {
        //        if (_finishLoad) {
        //            _progressView.hidden = YES;
        //        }
    }];
}
- (void)requestPart {
    NSString *reqUrl = _requestUrl;
    NSURL *url = [NSURL URLWithString:reqUrl];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [_webView loadRequest:request];
}

#pragma mark - 清除内存
- (void)cleanMemory {
    [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitDiskImageCacheEnabled"];//自己添加的，原文没有提到。
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitOfflineWebApplicationCacheEnabled"];//自己添加的，原文没有提到。
    [[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)shareAction {//分享
//    NSString *url = [NSString stringWithFormat:@"%@&sharedUser=%@",_requestUrl,[AppData share].username];
    NSString *url = [NSString stringWithFormat:@"%@/IM/NoticeDetail?id=%@",BaseUrl,self.identify];
    
    
    [[ShareView share] showMess:[NSString stringWithFormat:@"%@999%@",_titleStr,self.detailText] withImgUrl:_shareImgUrl url:url];
}

//加载完成
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [UIView animateWithDuration:1 animations:^{
        _progressView.frame = CGRectMake(_progressView.frame.origin.x, _progressView.frame.origin.y, ScreenWidth, 2);
    } completion:^(BOOL finished) {
        _finishLoad = YES;
        _progressView.hidden = YES;
        [_progressView removeFromSuperview];
    }];
    [self cleanMemory];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    _progressView.hidden = YES;
    if (kNetworkNotReachability) {
        [[MessShowView share] showWithMess:@"网络不给力"];
        _webView.hidden = YES;
        [_progressView setProgress:1 animated:YES];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
